import Login from "@/pages/system/login";
import { Button, Modal } from "@arco-design/web-react";
import { FC, ReactDOM, PureComponent, Fragment } from "react";

type TLoginAuthProps = FC | ReactDOM | any;

function LoginAuth(WrappedComponent: TLoginAuthProps) {
  return class LoginAuthComponent extends PureComponent<TLoginAuthProps, any> {
    constructor(props: TLoginAuthProps) {
      super(props);
      this.state = {
        visible: false,
        isAuthenticated: false,
        isLoading: false,
      }
    }
    componentDidMount() {
      // 这里可以调用你的认证服务，比如检查localStorage中的token
      const token = localStorage.getItem('token');
      this.setState({
        isAuthenticated: !!token,
        isLoading: true,
      });
    }
    render() {
      const { visible, isAuthenticated } = this.state;
      if (!isAuthenticated) {
        return (
          <Fragment>
          <Button type='default' long onClick={() => this.setState({ visible: true })}>登录</Button>
          <Modal
            visible={visible}
            onOk={() => this.setState({ visible: false })}
            onCancel={() => this.setState({ visible: false })}
            autoFocus={false}
            focusLock={true}
            footer={null}
            style={{width: "640px"}}
            unmountOnExit={true}
          >
            <Login onSussess={() => {
              this.setState({ visible: false })
              this.setState({ isAuthenticated: true })
            }}></Login>
          </Modal>
        </Fragment>
        );
      }
      return <WrappedComponent {...this.props} />;
    }
  }
}

export default LoginAuth;

